package com.ms.erpBase;

import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.ms.common.model.BusEmploee;
import com.ms.common.model.BusInfo;
import com.ms.common.model.BusOrder;
import com.ms.common.util.MsUtility;

public class BusOrderController extends Controller {

	public void createOrUpdate(){
		BusOrder be=getBean(BusOrder.class);
		if(be.getId()==null){
			be.setSignDate(new Date());
			if(be.getOrderStatus()==null||"".equals(be.getOrderStatus())){
				be.setOrderStatus("0");
			}
			be.save();
		}else{
			be.update();
		}
		renderJsp("/jsp/busOrder/list.jsp");
	}
	
	
	public void list(){
		Page<BusOrder> page = BusOrder.dao.findPageConditions(getModel(BusOrder.class),getParaToInt("page"),getParaToInt("rows") );
		renderJson(MsUtility.TransToEasyUiPage(page));
	}
	
	
	public void listForSelect(){
		Page<BusOrder> page = BusOrder.dao.findPage(getParaToInt("page"),getParaToInt("rows") );
		renderJson(MsUtility.TransToEasyUiPage(page));
		
	}
	
	public void edit(){
		String id=getPara("id");
		BusOrder be=BusOrder.dao.findById(id);
		setAttr("busOrder", be);
		renderJsp("/jsp/busOrder/form.jsp");
	}
	
	public void beginCreate(){
		renderJsp("/jsp/busOrder/form.jsp");
	}
	
	public void delete(){
		String ids=getPara("items");
		String[] idarr=ids.split(",");
		for (String id : idarr) {
			BusOrder.dao.deleteById(id);
		}
	}

	/**
	 * 查询出所有没有被安排出去车辆
	 * 按照时间查询
	 */
	public void listUnUsedBus(){
	 List<BusInfo> infoLst=	BusInfo.dao.find("SELECT * FROM bus_info WHERE id NOT IN (SELECT bus_id FROM bus_order WHERE begin_date<=NOW()  AND end_date>=NOW())");
	 renderJson(infoLst);
	}
	
	/**
	 * 查询出没有使用的司机
	 */
	public void lisUnUsedEmp(){
		String busId=getPara("busId");
		BusInfo bus=BusInfo.dao.findById(busId);
		if(bus.getIsOnlyDriver()=="1"||"1".equals(bus.getIsOnlyDriver())){
			List<BusEmploee> empLst= BusEmploee.dao.find("SELECT * from bus_emploee where id="+bus.getDriverId());
			renderJson(empLst);
		}else{//查询出所有没有出勤的司机
			List<BusEmploee> empLst= BusEmploee.dao.find("SELECT * FROM bus_emploee WHERE id NOT IN (SELECT driver_id FROM bus_order WHERE begin_date<=NOW()  AND end_date>=NOW())");
			renderJson(empLst);
		}
		
	}
	
	/**
	 * 订单生产
	 * 	orderId:<%=orderId %>,
    			busId:$('#busId').combobox('getValue'),
    			driverId:$('#driverId').combobox('getValue'),
    			driverName:$('#driverId').combobox('getText')
	 */
	public void orderSend(){
		String orderId=getPara("orderId");
		String driverName=getPara("driverName");
		
		BusOrder busOrder=BusOrder.dao.findById(orderId);
		busOrder.setDriverId(getParaToInt("driverId"));
		busOrder.setBusId(getPara("busId"));
		busOrder.setDriverName(driverName);
		busOrder.setOrderStatus("1");
		busOrder.update();
	}
}
